import sys,os,cgi,time
sys.path.insert(0, os.path.split(sys.path[0])[0])
import cfg

query = os.getenv("QUERY_STRING",'') 
kwargs = cgi.parse_qs(query)
hint = kwargs.get('hint',['1'])[0]

kind = os.getenv('AUTH_TYPE','')
user = os.getenv('REMOTE_USER','')
password = os.getenv('REMOTE_PASS','')
value = os.getenv('AUTH_VALUE','')

if hint=='1':
	print "HTTP/1.1 200 ok"
	print 'WWW-Authenticate: Basic realm="rivulet"'
	print "Content-Type: text/html"
	print
	print '<html><HEAD><META HTTP-EQUIV="refresh" CONTENT="2;URL=logout.py?hint=0"></HEAD>'
	print "<body><h1>use empty user or empty password to logout</h1></body></html>"
elif password and user:
	print "HTTP/1.1 401 Unauthorized"
	print 'WWW-Authenticate: Basic realm="rivulet"'
	print
else:
	print "HTTP/1.1 200 ok"
	print 'WWW-Authenticate: Basic realm="rivulet"'
	print "Content-Type: text/html"
	print
	print "<html><body><h1>logout ok</h1></body></html>"
